package Object.UserManager;

import java.util.Scanner;

public class Menu {
    Scanner inputScanner = new Scanner(System.in);
        private final UserManager userManager;

    public Menu(UserManager userManager){
        this.userManager = userManager;
    }

    public void showMainMenu(){
        while (true){
            printMenu();
            int sel = inputScanner.nextInt();
            inputScanner.nextLine();
            switch (sel){
                case 1:
                    registrationMenu();
                    break;
                case 2:
                    loginMenu();
                    break;
                case 0:
                    exit();
                    break;
                default:
                    System.out.println("重新选择");
            }
        }
    }


//四个方法
    private void printMenu(){
        System.out.println("\n---欢迎使用身份验证系统---");
        System.out.println("1.注册");
        System.out.println("2.登录");
        System.out.println("0.退出");
        System.out.println("请输入你的选择");
    }

    private void registrationMenu(){
        System.out.println("\n---用户注册---");
        System.out.println("输入用户名：");
        String userName = inputScanner.nextLine();
        System.out.println("输入密码");
        String password = inputScanner.nextLine();
        if (userManager.registerUser(userName, password)){
            System.out.println("注册成功！");
            userManager.printAllUsers();
        }else {
            System.out.println("用户名已存在！");
            userManager.printAllUsers();
        }
    }

    private void loginMenu(){
        System.out.println("---用户登录---");
        System.out.println("输入用户名");
        String userName = inputScanner.nextLine();
        System.out.println("输入密码：");
        String password = inputScanner.nextLine();
        if(userManager.loginUser(userName, password)){
            System.out.println("登录成功！");
        }else {
            System.out.println("认证失败！");
        }
    }

    private void exit() {
        System.out.println("退出系统...");
        System.exit(0); // 退出程序
    }
}
